package view.plugin
{
	//游戏结果
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import global.Define;
	import global.Resource;
	
	import model.GameModel;
	import model.MeInfoModel;
	
	import util.ImageNumber;
	
	public class GameResult extends EventDispatcher
	{
		private var displayObject:DisplayObjectContainer;
		private var item:Array = [];
		private var items:Array = [];
		private var baseScore:ImageNumber;
		private var doubleScore:ImageNumber;
		public static const DLG_CLOSE:String = "dlg_close";
		public function GameResult(obj:MovieClip)
		{
			displayObject = obj;
			
			items = [obj.item1, obj.item2, obj.item3];
			var tf:TextFormat = (obj.item1.nickName as TextField).defaultTextFormat;
			tf.color = 0xfacf0f;
			(obj.item1.nickName as TextField).defaultTextFormat = tf;
			var obj1:Object = null;
			var scoreSelf:ImageNumber = null;
			var scoreOther:ImageNumber = null;
			baseScore = new ImageNumber(Resource.i.getBitmapData("number01"));
			baseScore.signVisible = false;
			baseScore.x = 168; baseScore.y = 213;
			baseScore.scaleX  = 0.5; baseScore.scaleY  = 0.5;
			doubleScore = new ImageNumber(Resource.i.getBitmapData("number01"));
			doubleScore.signVisible = false;
			doubleScore.x = 415; doubleScore.y = 211;
			doubleScore.scaleX  = 0.5; doubleScore.scaleY  = 0.5;
			displayObject.addChild(baseScore);
			displayObject.addChild(doubleScore);
			var point:Array = [new Point(395, 96),new Point(395, 135),new Point(395, 172)];	
			for(var i:uint = 0; i < Define.GAME_PLAYER; i++)
			{
				var mc:MovieClip = items[i];
				scoreSelf = new ImageNumber(Resource.i.getBitmapData("number01"));
				scoreOther = new ImageNumber(Resource.i.getBitmapData("number02"));
				displayObject.addChild(scoreSelf);
				displayObject.addChild(scoreOther);
				scoreSelf.x = point[i].x; scoreSelf.y = point[i].y;
				scoreOther.x = point[i].x; scoreOther.y = point[i].y;
				scoreSelf.scaleX  = 0.8; scoreSelf.scaleY  = 0.8;
				scoreOther.scaleX  = 0.8; scoreOther.scaleY  = 0.8;
				obj1 = {};
				obj1.name = mc.nickName;
				obj1.succeed = mc.succeed;
				obj1.scoreSelf = scoreSelf;
				obj1.scoreOther = scoreOther;
				item[i] = obj1;
			}
			
			displayObject.addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
		}
		public function updateData():void
		{
			if(!displayObject) return;
			
//			GameModel.i.baseScore = 200;
//			GameModel.i.curBrightTime = 128
//			GameModel.i.gameScore[0] = -9658;
//			GameModel.i.gameScore[1] = 3698;
//			GameModel.i.gameScore[2] = 3698;
//			GameModel.i.userAccount[0] = "速度来看房时";
//			GameModel.i.userAccount[1] = "士大夫";
//			GameModel.i.userAccount[2] = "sdfsdfsdf";
//			MeInfoModel.i.userStatus.wChairID = 0;
			
			baseScore.value = GameModel.i.baseScore;
			doubleScore.value = GameModel.i.curBrightTime;
			var isWin:Boolean = GameModel.i.gameScore[MeInfoModel.i.userStatus.wChairID] > 0;
			var meChair:int = MeInfoModel.i.userStatus.wChairID;
			var score:Array = GameModel.i.gameScore.slice();
			var name:Array = GameModel.i.userAccount.slice();
			score.unshift(score.splice(meChair,1)[0]);
			name.unshift(name.splice(meChair,1)[0]);
			for(var i:uint = 0; i < Define.GAME_PLAYER; i++)
			{
				var isSelf:Boolean = i == 0;
				item[i].name.text = name[i];
				item[i].scoreSelf.visible = isSelf;
				item[i].scoreOther.visible = !isSelf;
				item[i].scoreSelf.value = score[i];
				item[i].scoreOther.value = score[i];
				if(score[i] > 0)
				{
					item[i].succeed.gotoAndStop(isSelf ? 1 : 2);
				}
				else
				{
					item[i].succeed.gotoAndStop(isSelf ? 3 : 4);
				}
			}
			
			
		}
		public function show(visible:Boolean):void
		{
			if(visible)
				ChessLobby.dialog.addChild(displayObject);
			else if(displayObject.parent)
				displayObject.parent.removeChild(displayObject);
			
			ChessLobby.masks.visible = visible;
		}
		
		public function onAddToStage(evt:Event):void
		{
			displayObject.x = (Define.SCREEN_WIDTH - displayObject.width) / 2;
			displayObject.y = (Define.SCREEN_HEIGHT - displayObject.height) / 2 - 80;
		}
	}
}